//
//  Release_Job.m
//  HappyStore
//
//  Created by 李小辉 on 16/6/15.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "Release_Job.h"
#import "JobView.h"
@interface Release_Job ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) JobView *jobShiTu;

//地区
@property (strong, nonatomic) NSString *provinceStr;
@property (strong, nonatomic) NSString *cityStr;
@property (strong, nonatomic) NSString *districtStr;
//工作年限
@property (strong, nonatomic) NSArray *experienceAry;
//行业方向
@property (strong, nonatomic) NSArray *aryHYtitle;
//工作性质
@property (strong, nonatomic) NSArray *natureWorkAry;
//薪资选择
@property (strong, nonatomic) NSArray *moneyAty;
//学历
@property (strong, nonatomic) NSArray *educationalAry;
//判断选择的按钮
@property (assign, nonatomic) NSInteger number;

@property (strong, nonatomic) SelectedAddress *selectAddress;
@property (strong, nonatomic) SingleSelectedDialog *tableViewIndustry;
@end

@implementation Release_Job

- (NSArray *)moneyAty{
    if (!_moneyAty) {
        _moneyAty = @[@"面议",@"1K以下",@"1K~3K",@"3K~5K",@"5K~7K",
                      @"7K~9K",@"10K~15K",@"15K~30K",@"30K以上"];
    }
    return _moneyAty;
}

- (NSArray *)natureWorkAry{
    if (!_natureWorkAry) {
        _natureWorkAry = @[@"不限",@"实习",@"全职",@"兼职"];
    }
    return _natureWorkAry;
}

- (NSArray *)educationalAry{
    if (!_educationalAry) {
        _educationalAry = @[@"初中",@"高中",@"专科",@"本科",@"硕士",@"博士"];
    }
    return _educationalAry;
}
- (NSArray *)experienceAry{
    if (!_experienceAry) {
        _experienceAry = @[@"一年以下",@"1-3年",@"3-5年",@"5-10年",@"10年以上",@"不限"];
    }
    return _experienceAry;
}
- (NSArray *)aryHYtitle{
    if (!_aryHYtitle) {
        _aryHYtitle = @[@"生物医药",@"旅游休闲",@"制造业",@"批发零售",
                        @"房地产业",@"交通运输",@"冶金矿业",@"建筑建材",
                        @"信息软件",@"金融",@"教育",@"现代服务业",
                        @"生态环保",@"市政基建",@"文体娱乐",@"社会保障"
                        ,@"水电燃气",@"农林牧渔",@"其他行业"];
    }
    return _aryHYtitle;
}

- (JobView *)jobShiTu{
    if (!_jobShiTu) {
        _jobShiTu = [[[NSBundle mainBundle] loadNibNamed:@"JobView" owner:self options:nil] lastObject];
    }
    return _jobShiTu;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"白色背景"] forBarMetrics:UIBarMetricsDefault];
    //找出导航栏的下边的黑线
    UIImageView *navBarHairlineImageView = [NaviBarLine findHairlineImageViewUnder:self.navigationController.navigationBar];
    navBarHairlineImageView.hidden = NO;
    navBarHairlineImageView.alpha = 0.5f;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    //找出导航栏的下边的黑线
    UIImageView *navBarHairlineImageView = [NaviBarLine findHairlineImageViewUnder:self.navigationController.navigationBar];
    navBarHairlineImageView.hidden = YES;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.text = @"发布职位";
    titleLabel.textColor = RGB(45, 45, 45);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    
    // 设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"返回" highImage:@"返回"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = NO;
    //点击行业按钮
    [self.jobShiTu.industryBtn addTarget:self action:@selector(clickIndustyBtn:) forControlEvents:UIControlEventTouchUpInside];
    //点击工作性质按钮
    [self.jobShiTu.workNatureBtn addTarget:self action:@selector(clickSkillLabelBtn:) forControlEvents:UIControlEventTouchUpInside];
    //点击学历按钮
    [self.jobShiTu.educationalBtn addTarget:self action:@selector(clickEducationalBtn:) forControlEvents:UIControlEventTouchUpInside];
    //点击工作年限按钮
    [self.jobShiTu.jobYearBtn addTarget:self action:@selector(clickJobYearBtn:) forControlEvents:UIControlEventTouchUpInside];
    //点击工作地区按钮
    [self.jobShiTu.jobAdressBtn addTarget:self action:@selector(clickJobAdressBtn:) forControlEvents:UIControlEventTouchUpInside];
    //点击薪资按钮
    [self.jobShiTu.moneyBtn addTarget:self action:@selector(clickMoneyBtn) forControlEvents:UIControlEventTouchUpInside];
    //加载view
    [self setupJobView];
    //发布按钮事件
    [self.jobShiTu.FabuBtn addTarget:self action:@selector(sendFabuBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    self.tableViewIndustry = [SingleSelectedDialog sharedSelectedAddress];
    self.tableViewIndustry.delegatee = self;
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}
-(void)leftBarButtonItemClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)setupJobView{
    self.jobShiTu.frame = CGRectMake(0, 0, screenW, self.jobShiTu.height);
    [self.scrollView addSubview:self.jobShiTu];
    self.scrollView.contentSize = CGSizeMake(screenW, self.jobShiTu.height);
    
}

#pragma mark - 发布视图上的按钮

//点击行业按钮
- (void)clickIndustyBtn:(UIButton *)btn{
    self.number = 1;
    NSMutableArray *muAry = [NSMutableArray array];
    for (int i=0; i<self.aryHYtitle.count; i++) {
        [muAry addObject:self.aryHYtitle[i]];
    }
    [self.tableViewIndustry btnClickAddressWithData:muAry];
}


//点击学历按钮
- (void)clickEducationalBtn:(UIButton *)btn{
    self.number = 2;
    NSMutableArray *muAry = [NSMutableArray array];
    for (int i=0; i<self.educationalAry.count; i++) {
        [muAry addObject:self.educationalAry[i]];
    }
    [self.tableViewIndustry btnClickAddressWithData:muAry];
}

//点击工作年限按钮
- (void)clickJobYearBtn:(UIButton *)btn{
    self.number = 3;
    NSMutableArray *muAry = [NSMutableArray array];
    for (int i=0; i<self.experienceAry.count; i++) {
        [muAry addObject:self.experienceAry[i]];
    }
    [self.tableViewIndustry btnClickAddressWithData:muAry];
}
//点击工作性质按钮
- (void)clickSkillLabelBtn:(UIButton *)btn{
    self.number = 4;
    NSMutableArray *muAry = [NSMutableArray array];
    for (int i = 0; i<self.natureWorkAry.count; i++) {
        [muAry addObject:self.natureWorkAry[i]];
    }
    [self.tableViewIndustry btnClickAddressWithData:muAry];
    
}
//点击薪资按钮
- (void)clickMoneyBtn{
    self.number = 5;
    NSMutableArray *muAry = [NSMutableArray array];
    for (int i = 0; i<self.moneyAty.count; i++) {
        [muAry addObject:self.moneyAty[i]];
    }
    [self.tableViewIndustry btnClickAddressWithData:muAry];
}
//点击工作地区按钮
- (void)clickJobAdressBtn:(UIButton *)btn{
    self.selectAddress = [SelectedAddress sharedSelectedAddress];
    self.selectAddress.delegatee = self;
    [self.selectAddress btnClickAddress];
}
//地区协议方法
- (void)seceltedRowSshowMsg:(NSString *)showMsg andProvince:(NSString *)strProvince andCity:(NSString *)strCity andDistrict:(NSString *)strDistrict{

    self.provinceStr = strProvince;
    self.cityStr = strCity;
    self.districtStr = strDistrict;
    
    self.jobShiTu.jobAdressLabel.text = [NSString stringWithFormat:@"%@/%@/%@",self.provinceStr,self.cityStr,self.districtStr];
}
//
- (void)seceltedRow:(NSInteger)rrow{
    switch (self.number) {
        case 1://行业
            self.jobShiTu.industryLabel.text = self.aryHYtitle[rrow];
            break;
        case 2://学历
            self.jobShiTu.educationalLabel.text = self.educationalAry[rrow];
            break;
        case 3://年限
            self.jobShiTu.jobYearLabel.text = self.experienceAry[rrow];
            break;
        case 4://性质
            self.jobShiTu.workNatureLabel.text = self.natureWorkAry[rrow];
            break;
        case 5://薪资
            self.jobShiTu.moneyLabel.text = self.moneyAty[rrow];
            break;
        default:
            break;
    }
}



#pragma mark - 发布按钮
- (void)sendFabuBtn:(UIButton *)sender {
    
    BOOL isOK = YES;
   
    if (isOK && self.jobShiTu.industryLabel.text.length == 0) {
        isOK = NO;
        [self showSingleIconBubble:@"请选择行业"];

    }
    if (isOK && self.jobShiTu.jobTitleField.text.length == 0) {
        isOK = NO;
        [self showSingleIconBubble:@"请输入职位"];

    }

    
    if (isOK && [self.jobShiTu.jobYearLabel.text isEqualToString:@"必选"]) {
        isOK = NO;
        [self showSingleIconBubble:@"请选择工作经验"];

    }
    
    if (isOK && [self.jobShiTu.moneyLabel.text isEqualToString:@"必选"]) {
        isOK = NO;
       [self showSingleIconBubble:@"请选择薪资"];

    }
    
    if (isOK && self.jobShiTu.comanyTextField.text.length == 0) {
        isOK = NO;
        [self showSingleIconBubble:@"请填写公司名称"];

    }
    
    
    if (isOK && [self.jobShiTu.jobAdressLabel.text isEqualToString:@"必填"]) {
        isOK = NO;
        [self showSingleIconBubble:@"请选择地区"];

    }
    
    if (isOK) {
        if ([self.jobShiTu.dicTextView.text isEqualToString:@"请对你发布的内容进行一个详细的描述"] || [self.jobShiTu.dicTextView.text isEqualToString:@""] ) {
            isOK = NO;
            [self showSingleIconBubble:@"请输入职位描述"];

        }
    }
    
    
    
    if (isOK) {
        //addJobhunting_app_jobhunting.im.lensum?token=XF2YkxodEFLmVot6svnA&userId=9&type=2&title=daiwozou&positionName=ios&address=cqnp&industry=hlw&money=15000&schooling=bk&experience=1-3&description=yaolewoba&workNature=实习&skillLable=技能&recruitCompany=招聘公司    workNature:工作性质-> 实习 全职  兼职
        //token=验证用户&userId=用户id&type=类型（1招聘 2求职）&title=标题&positionName=职位&address=工作地点&industry=行业&money=期望薪资&schooling=学历&experience=工作年限&description=描述 参数：photos-为图片zip包
        [self showRoundProgressWithTitle:@"请稍后..."];
        NSMutableDictionary *muDicParameters = [NSMutableDictionary dictionary];
        [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"token"] forKey:@"token"];//验证用户
        [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"userid"] forKey:@"userId"];
        [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"usertype"] forKey:@"useType"];//用户类型
        
        [muDicParameters setObject:@(1) forKey:@"type"];//类型（1招聘 2求职）
        
        
        
        if (self.jobShiTu.moneyLabel.text.length != 0) {//根据薪资
            for (int i = 0; i<self.moneyAty.count; i++) {
                if ([self.jobShiTu.moneyLabel.text isEqualToString:self.moneyAty[i]]) {
                    //@"面议",@"1K以下",@"1K~3K",@"3K~5K",@"5K~7K",
                    //@"7K~9K",@"10K~15K",@"15K~30K",@"30K以上"
                    switch (i) {
                        case 0:
                        {
                            [muDicParameters setObject:@"0" forKey:@"money"];
                        }
                            break;
                        case 1:
                        {
                            [muDicParameters setObject:@"500" forKey:@"money"];
                        }
                            break;
                        case 2:
                        {
                            [muDicParameters setObject:@"2000" forKey:@"money"];
                        }
                            break;
                        case 3:
                        {
                            [muDicParameters setObject:@"4000" forKey:@"money"];
                        }
                            break;
                        case 4:
                        {
                            [muDicParameters setObject:@"6000" forKey:@"money"];
                        }
                            break;
                        case 5:
                        {
                            [muDicParameters setObject:@"8000" forKey:@"money"];
                        }
                            break;
                        case 6:
                        {
                            [muDicParameters setObject:@"12000" forKey:@"money"];
                        }
                            break;
                        case 7:
                        {
                            [muDicParameters setObject:@"18000" forKey:@"money"];
                        }
                            break;
                        case 8:{
                            [muDicParameters setObject:@"30000" forKey:@"money"];
                        }
                            break;
                    }
                }
            }
            
        }

        
        
        //[muDicParameters setObject:self.jobShiTu.moneyTextField.text forKey:@"money"];//薪资
        [muDicParameters setObject:self.jobShiTu.workNatureLabel.text forKey:@"workNature"];
//        if ([self.provinceStr isEqualToString:self.cityStr]) {
//            [muDicParameters setObject:[NSString stringWithFormat:@"%@%@/%@",self.cityStr,self.districtStr,self.jobShiTu.decAdressTextField.text]forKey:@"address"];
//             }else{
//                 [muDicParameters setObject:[NSString stringWithFormat:@"%@%@%@/%@",self.provinceStr,self.cityStr,self.districtStr,self.jobShiTu.decAdressTextField.text] forKey:@"address"];//工作地点
//             }
        NSString *stringAdr = [NSString stringWithFormat:@"%@-%@-%@/%@",self.provinceStr,self.cityStr,self.districtStr,self.jobShiTu.decAdressTextField.text];
        [muDicParameters setObject:stringAdr forKey:@"address"];
        [muDicParameters setObject:self.jobShiTu.comanyTextField.text forKey:@"recruitCompany"];
        
        [muDicParameters setObject:self.jobShiTu.emailTextField.text forKey:@"email"];//邮箱
        [muDicParameters setObject:self.jobShiTu.industryLabel.text forKey:@"industry"];//industry=行业
        [muDicParameters setObject:self.jobShiTu.jobTitleField.text forKey:@"positionName"];//职位
        [muDicParameters setObject:self.jobShiTu.educationalLabel.text forKey:@"schooling"];//学历
        [muDicParameters setObject:self.jobShiTu.jobYearLabel.text forKey:@"experience"];//工作年限
        
        //&lable=标签&description=描述 参数：photo-为logo缩略图、photos-为图片zip包
        [muDicParameters setObject:self.jobShiTu.dicTextView.text forKey:@"description"];
       
        
        
        
        NSString *strURl = [NSString stringWithFormat:@"%@%@",URL_DEFINE,@"addJobhunting_app_jobhunting.im.lensum"];
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        [manager POST:strURl parameters:muDicParameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

            
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
            //NSLog(@"%@",dicInfo);

            if ([dicInfo[@"result"] integerValue] == 1) {

                [self showRightWithTitle:dicInfo[@"msg"] autoCloseTime:2];
                [self.navigationController popViewControllerAnimated:YES];
                
            }else {

                [self showSingleIconBubble:dicInfo[@"msg"]];
            }

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

            [self showSingleIconBubble:error.localizedDescription];

        }];

    }
    
    

}




@end
